package com.itheima.mm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mm.constant.MessageConstant;
import com.itheima.mm.context.BaseContext;
import com.itheima.mm.dto.CourseDTO;
import com.itheima.mm.entity.CoursePageQueryParams;
import com.itheima.mm.entity.PageResult;
import com.itheima.mm.entity.QueryPageBean;
import com.itheima.mm.exception.BaseException;
import com.itheima.mm.mapper.*;
import com.itheima.mm.pojo.Course;
import com.itheima.mm.service.CourseService;
import com.itheima.mm.utils.DateUtils;
import com.itheima.mm.vo.CourseAddVO;
import com.itheima.mm.vo.CourseVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description CourseServiceImpl
 * @Author mylee
 * @Date 2023-09-26
 */
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CatalogMapper catalogMapper;
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 新增学科
     *
     * @return
     * @param: courseAllVO
     **/

    @Override
    public void add(CourseAddVO courseAddVO) {
        Course course = new Course();

        BeanUtils.copyProperties(courseAddVO, course);

        course.setCreateDate(String.valueOf(LocalDateTime.now()));
        course.setUserId(BaseContext.getCurrentId());

        courseMapper.insert(course);
    }

    /**
     * 学科列表
     *
     * @return
     * @param: coursePageQueryParamsQueryPageBean
     **/
    @Override
    public PageResult<Course> pageQuery(QueryPageBean<CoursePageQueryParams> queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Course> courseList = courseMapper.findByCondition(queryPageBean.getQueryParams());
        Page<Course> page = (Page<Course>) courseList;

        List<CourseVO> courseVOS = courseList.stream().map(course -> {
            CourseVO courseVO = new CourseVO();
            BeanUtils.copyProperties(course, courseVO);
            Integer courseId = course.getId();
            Integer countByName = catalogMapper.countByName(courseId);
            Integer countByTag = tagMapper.countByTag(courseId);
            Integer countByQuestion = questionMapper.countByQuestion(courseId);
            Integer userId = course.getUserId();
            String creator = userMapper.findByUserId(userId);

            courseVO.setCatalogQty(countByName);
            courseVO.setQuestionQty(countByQuestion);
            courseVO.setTagQty(countByTag);
            courseVO.setCreateDate(DateUtils.parseDate2String(new Date()));
            courseVO.setCreator(creator);
            return courseVO;
        }).collect(Collectors.toList());
        return new PageResult(page.getTotal(), courseVOS);
    }

    /**
     * 学科更新
     *
     * @return
     * @param: course
     **/
    @Override
    public void update(CourseDTO courseDTO) {
        Course course = new Course();
        BeanUtils.copyProperties(courseDTO, course);
        courseMapper.update(course);
    }

    /**
     * 删除学科
     *
     * @return
     * @param: courseId
     **/
    @Override
    public void deleteByCourse(Integer courseId) {
        Integer count = catalogMapper.countByName(courseId);
        if (count > 0) {
            throw new BaseException(MessageConstant.CATALOG_ON_SALE);
        }
       count =  tagMapper.countByTag(courseId);
        if (count > 0) {
            throw new BaseException(MessageConstant.TAG_ON_SALE);
        }
        count = questionMapper.countByQuestion(courseId);
        if (count > 0){
            throw  new BaseException(MessageConstant.QUESTION_BY_SALE);
        }
        count = courseMapper.countByStatus(courseId);
        if (count > 0){
            throw  new BaseException(MessageConstant.COURSE_IS_SHOW);
        }
        courseMapper.deleteById(courseId);
    }
}
